package com.itheima;

import com.itheima.pojo.Address;
import com.itheima.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController  // @Controller + @ResponseBody
public class HelloController {

    //    简单参数
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestParam("username") String name, Integer age) {
        //如果前端参数和后端接收到的参数名字不一样，可以使用@RequestParam指定参数名字
        System.out.println("name=" + name + ", age=" + age);
        return "OK:" + name + ":" + age;
    }


    // 简单实体对象
    @RequestMapping("/simplePojo")
    public String simplePojo(User user) {
        System.out.println("接收到前端数据：" + user);
        return "OK" + user;
    }

    //复杂参数
    @RequestMapping("/complexPojo")
    public String complexPojo(User user) {
        System.out.println("接收到前端数据：" + user);
        return "OK" + user;
    }

    //数组接收
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobbies) {
        System.out.println(Arrays.toString(hobbies));
        return "OK:" + Arrays.toString(hobbies);
    }

    //集合接收
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobbies) {
        System.out.println(hobbies);
        return "OK:" + hobbies;
    }

    //日期参数
    @RequestMapping("/dateParam")
    public String dateParam(
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate birthday) {
        System.out.println("updateTime = " + updateTime);
        System.out.println("birthday = " + birthday);
        return "OK:" + updateTime + "," + birthday;
    }


    //json数据
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user) {
        System.out.println(user);
        return "OK," + user;
    }

    //路径参数
    @RequestMapping("/path/{id}/{name}")
    public String pathParam(@PathVariable Integer id, @PathVariable String name) {
        System.out.println("id=" + id + ",name=" + name);
        return "OK," + id + "," + name;
    }

    @RequestMapping("/getUser")
    public User getUser() {
        Address address = new Address();
        address.setProvince("福建省");
        address.setCity("厦门市");

        User user = new User();
        user.setName("张三");
        user.setAge(18);
        user.setAddress(address);
        return user;
    }
    @RequestMapping("/getUsers")
    public List<User> getUsers() {
        Address address = new Address();
        address.setProvince("福建省");
        address.setCity("厦门市");

        User user = new User();
        user.setName("张三");
        user.setAge(18);
        user.setAddress(address);

        //集合创建保存多个数据返回
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        users.add(user);
        users.add(user);
        users.add(user);

        return users;
    }

}
